﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using FarmSmartSleep.Common.Domain.RBACFile;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherWeaningFile
{
    /// <summary>
    /// 断奶查询
    /// </summary>
    public class MotherWeaningListHandler:IRequestHandler<MotherWeaningListCommon, ResultPage<List<MotherWeaningListDto>>>
    {
        private readonly IBaseRepository<MotherWeaning> motherWeaningRep;
        private readonly IBaseRepository<SleepInfor> _sleepinforRep;
        private readonly IBaseRepository<Cottages> cottagesRep;
        private readonly IBaseRepository<Fields> fieldsRep;
        private readonly IBaseRepository<Employee> employeeRep;
        /// <summary>
        /// 断奶构造函数
        /// </summary>
        /// <param name="motherWeaningRep"></param>
        /// <param name="sleepinforRep"></param>
        /// <param name="cottagesRep"></param>
        /// <param name="fieldsRep"></param>
        public MotherWeaningListHandler(IBaseRepository<MotherWeaning> motherWeaningRep, IBaseRepository<SleepInfor> sleepinforRep, IBaseRepository<Cottages> cottagesRep, IBaseRepository<Fields> fieldsRep, IBaseRepository<Employee> employeeRep)
        {
            this.motherWeaningRep = motherWeaningRep;
            _sleepinforRep = sleepinforRep;
            this.cottagesRep = cottagesRep;
            this.fieldsRep = fieldsRep;
            this.employeeRep = employeeRep;
        }

        public async Task<ResultPage<List<MotherWeaningListDto>>> Handle(MotherWeaningListCommon request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<List<MotherWeaningListDto>>() { Code = (int)CodeEnum.search };

            try
            {
                var motherWeaning = motherWeaningRep.GetAll().Where(x => x.Deletes == false);
                var sleepinfor = _sleepinforRep.GetAll();
                var cottages = cottagesRep.GetAll();
                var fields = fieldsRep.GetAll();
                var employee = employeeRep.GetAll();
                var listQuery = (from s in sleepinfor
                                 join m in motherWeaning
                                 on s.SleepInforId equals m.SowId
                                 join c in cottages on m.CurrentBarn equals c.CottagesId
                                 join f in fields on m.CurrentPen equals f.FieIdId 
                                 select new MotherWeaningListDto
                                 {
                                     MotherWeaningId = m.MotherWeaningId,
                                     Leader = m.Leader,
                                     WeaningDate = m.WeaningDate,
                                     SowId = s.SleepInforId,
                                     EweName = s.EarNumber,
                                     BreastfeedingDays = m.BreastfeedingDays,
                                     WeanedQuantity = m.WeanedQuantity,
                                     TotalWeaningWeight = m.TotalWeaningWeight,
                                     MotherWeight = m.MotherWeight,
                                     TransferBarnId = c.CreateId,
                                     TransferBarn = c.CottageName,
                                     TransferPenId = f.FieIdId,
                                     TransferPen = f.FieIdName, 
                                     Notes = m.Notes,
                                     DocumentNo = m.DocumentNo,
                                     SerialNo = m.SerialNo, 
                                     WeaningSeason = m.WeaningSeason,
                                     CreateId = m.CreateId,
                                     CreateTime = m.CreateTime,
                                     UpdateId = m.UpdateId,
                                     UpdateTime = m.UpdateTime,
                                     ExamId = m.ExamId,
                                     ExamTime = m.ExamTime,
                                 }).AsQueryable();
                if (!string.IsNullOrEmpty(request.Leader))
                {
                    listQuery = listQuery.Where(x => x.Leader.Contains(request.Leader));
                } 
                if (request.TransferBarn != 0)
                {
                    listQuery = listQuery.Where(x => x.TransferBarnId == request.TransferBarn);
                } 
                if (request.MinDate != null)
                {
                    listQuery = listQuery.Where(x => x.WeaningDate >= request.MinDate);
                }
                if (request.MaxDate != null)
                {
                    listQuery = listQuery.Where(x => x.WeaningDate <= request.MaxDate);
                }
                if (request.EweID != 0)
                {
                    listQuery = listQuery.Where(x => x.SowId == request.EweID);
                }
                if (listQuery == null || !listQuery.Any())
                {
                    result.Messege = "暂无数据";
                    return result;
                }
                //仅在筛选后转换为列表
                var list = listQuery.ToList();
                result.totalCount = list.Count();
                result.totalPage = (int)Math.Ceiling(result.totalCount / (double)request.PageSize);
                list = list.OrderBy(c => c.MotherWeaningId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Code = (int)CodeEnum.success;
                result.Item = list;
                result.Messege = "查询成功";


            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
